package cn.stylefeng.guns.modular.rms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.rms.entity.Outtask;
import cn.stylefeng.guns.modular.rms.entity.OuttaskApply;
import cn.stylefeng.guns.modular.rms.mapper.OuttaskApplyMapper;
import cn.stylefeng.guns.modular.rms.mapper.OuttaskMapper;
import cn.stylefeng.guns.modular.rms.model.params.OuttaskApplyParam;
import cn.stylefeng.guns.modular.rms.model.params.OuttaskParam;
import cn.stylefeng.guns.modular.rms.model.result.OuttaskApplyResult;
import  cn.stylefeng.guns.modular.rms.service.OuttaskApplyService;
import cn.stylefeng.guns.modular.rms.service.OuttaskService;
import cn.stylefeng.guns.modular.ums.mapper.MemberMapper;
import cn.stylefeng.guns.modular.ums.model.params.MemberParam;
import cn.stylefeng.guns.modular.ums.model.result.MemberResult;
import cn.stylefeng.guns.modular.ums.service.MemberService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 外包任务报名 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2022-04-28
 */
@Service
public class OuttaskApplyServiceImpl extends ServiceImpl<OuttaskApplyMapper, OuttaskApply> implements OuttaskApplyService {

    @Resource
    private OuttaskMapper outtaskMapper;
    @Resource
    private MemberMapper memberMapper;

    @Override
    public void add(OuttaskApplyParam param){
        OuttaskApply entity = getEntity(param);
        this.save(entity);
        //添加报名次数
        Outtask outtask=outtaskMapper.selectById(param.getTaskId());
        outtask.setNumber(outtask.getNumber()+1);
        outtaskMapper.updateById(outtask);
    }

    @Override
    public void delete(OuttaskApplyParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(OuttaskApplyParam param){
        //根据会员id和任务id判断是否已报名
        OuttaskApplyResult result=this.findBySpec(param);
        if (result!=null){
            //修改报名信息
            param.setId(result.getId());
            OuttaskApply oldEntity = getOldEntity(param);
            OuttaskApply newEntity = getEntity(param);
            ToolUtil.copyProperties(newEntity, oldEntity);
            this.updateById(newEntity);
        }else{
            this.add(param);
        }
    }

    @Override
    public OuttaskApplyResult findBySpec(OuttaskApplyParam param){
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<OuttaskApplyResult> findListBySpec(OuttaskApplyParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(OuttaskApplyParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        List<OuttaskApplyResult> list=page.getRecords();
        if(param.getTaskId()==null){//查询用户申请任务
            for (int i=0;i<list.size();i++){
                list.get(i).setOuttask(outtaskMapper.findBySpec(new OuttaskParam().setId(list.get(i).getTaskId())));
            }
        }else{//查询任务报名列表
            MemberResult memberResult;
            for (int i=0;i<list.size();i++){
                memberResult=memberMapper.getMemberById(list.get(i).getMemberId());
                if (memberResult!=null){
                    list.get(i).setUsername(memberResult.getName());
                    list.get(i).setAvatar(memberResult.getAvatar());
                }
            }
        }
        page.setRecords(list);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageBySpecForAdmin(OuttaskApplyParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageListForAdmin(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(OuttaskApplyParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private OuttaskApply getOldEntity(OuttaskApplyParam param) {
        return this.getById(getKey(param));
    }

    private OuttaskApply getEntity(OuttaskApplyParam param) {
        OuttaskApply entity = new OuttaskApply();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
